import java.util.Random;


public class PlayBoard extends Board {
		
	//inputs
	//should be set to true on key down and will be reset when proceed by runFrame
	private boolean inputRotateCW;
	private boolean inputRotateCCW;
	private boolean inputHardDrop;
	private boolean inputHold;
	private boolean inputDown; 
	private boolean inputLeft; 
	private boolean inputRight;
	//should be set to true on key down, false on key up
	private boolean heldDown;
	private boolean heldLeft;
	private boolean heldRight;
	private long inputTime = 0; // used for DAS (only for Left and Right)
	private boolean isRightLastInput; // true if Right was pressed last (when both key are held)
	
	private Random rndGen; //used to generate next tetros
	
	private boolean gameOverPlay; // keep playing solo after game over (multiplayer)
	
	// in runFrame based on the current tetroState, if currentTime >= nextStateTime :
	//	insert a new tetro
	//	make the tetro fall one row
	//	lock the tetro
	//	clear the lines
	//	enter waiting state
	// if paused, contains a delta to be added to the current time when unpausing
	private long nextStateTime;
	private int currentFallDelay; // based on FALLING_LENGTH and current level
	
	public PlayBoard(int lvl, boolean gameOverPlay, long rndSeed) {
		paused = true;
		nextStateTime = 0;
		setLevel(lvl);
		tetroState = TetroState.WAITING;
		score = 0;
		nbLinesDone = 0;
		this.gameOverPlay = gameOverPlay;
		rndGen = new Random(rndSeed);
		board = new byte[ROWS][COLUMNS];
	}
	
	public PlayBoard(int lvl, boolean gameOverPlay) {
		this(lvl, gameOverPlay, System.currentTimeMillis());
	}
	
	public boolean isInputLeft() {
		return inputLeft || heldLeft;
	}

	public void setInputLeft(boolean inputLeft) {
		if (inputLeft) {
			this.inputLeft = true;
		}
		heldLeft = inputLeft;
		isRightLastInput = !inputLeft;
		inputTime = System.currentTimeMillis();
	}

	public boolean isInputRight() {
		return inputRight || heldRight;
	}

	public void setInputRight(boolean inputRight) {
		if (inputRight) {
			this.inputRight = true;
		}
		heldRight = inputRight;
		isRightLastInput = inputRight;
		inputTime = System.currentTimeMillis();
	}

	public boolean isInputDown() {
		return inputDown;
	}

	public void setInputDown(boolean inputDown) {
		if (inputDown) {
			this.inputDown = true;
		}
		heldDown = inputDown;
	}

	public boolean isInputRotateCW() {
		return inputRotateCW;
	}

	public void setInputRotateCW() {
		this.inputRotateCW = true;
	}

	public boolean isInputRotateCCW() {
		return inputRotateCCW;
	}

	public void setInputRotateCCW() {
		this.inputRotateCCW = true;
	}

	public boolean isInputHardDrop() {
		return inputHardDrop;
	}

	public void setInputHardDrop() {
		this.inputHardDrop = true;
	}

	public boolean isInputHold() {
		return inputHold;
	}

	public void setInputHold() {
		this.inputHold = true;
	}
	
	public void resetInputs() {
		inputLeft = false;
		inputRight = false;
		inputDown = false;
		heldDown = false;
		heldRight = false;
		heldLeft = false;
		inputRotateCW = false;
		inputRotateCCW = false;
		inputHardDrop = false;
		inputHold = false;
	}

	public void setLevel(int lvl) {
		if (lvl<1) {
			lvl = 1;
		}
		level = lvl;
		currentFallDelay = FALLING_LENGTH; // FIXME make a formula
	}
	
	public void setPaused(boolean pause) {
		if (pause != paused) {
			long time = System.currentTimeMillis();
			if (pause) {
				nextStateTime = nextStateTime - time;
			} else {
				nextStateTime = time + nextStateTime;
			}
		}
		resetInputs();
	}
	
	private void genNextTetros() {
		//TODO
	}
	
	public int runFrame() {
	//perform moves, rotations, tetro fall, etc based on inputs and current time
	// should be called repeatedly at a fixed tick rate
	//returns update flags if any event occurred
		
		if (paused) {
			return 0;
		}
		
		long time = System.currentTimeMillis();
		
		
		return 0;
	}
	
	public static void main(String[] args) {
		
	}

}
